﻿@model PrjAssessModels.BaseModels.Project



@section scripts{
    <script type="text/javascript">
        $(function ()
        {
            
            var viewModelJson = '@Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(this.Model))';
            var WorkItem;
            var viewModelWorkItem = {
                'Works': {
                    create: function (options)
                    {
                        WorkItem = ko.mapping.fromJS(options.data);
                        WorkItem.PriceTotal = ko.computed(function () { try { return (parseFloat(this.Price()) * parseFloat(this.Number())).toFixed(2) } catch (e) { return 0; }; }, WorkItem);
                        WorkItem.Sum = ko.computed(function () { try { return (parseFloat(this.Price()) * parseFloat(this.Number()) * parseFloat(this.PredictNo())).toFixed(2) } catch (e) { return 0; }; }, WorkItem);
                        return WorkItem;
                    }
                }
            };
            var viewModel = ko.mapping.fromJSON(viewModelJson, viewModelWorkItem);
            //---------------------添加项---------------------
            viewModel.addWorkItem = function ()
            {

                var item = {
                    PrjId: viewModel.PrjId(),
                    ContentName: "新的分项内容",
                    Price: 0,
                    Number: 0,
                    PredictNo: 0,
                    PriceTotal: 0
                };
                var itemVm = ko.mapping.fromJS(item);
                itemVm.PriceTotal = ko.computed(function () { try { return (parseFloat(this.Price()) * parseFloat(this.Number())).toFixed(2) } catch (e) { return 0; }; }, itemVm);
                itemVm.Sum = ko.computed(function () { try { return (parseFloat(this.PriceTotal()) * parseFloat(this.PredictNo())).toFixed(2) } catch (e) { return 0; }; }, itemVm);

                viewModel.Works.push(itemVm);
            }.bind(viewModel);
            //---------------------删除项---------------------
            viewModel.removeWorkItem = function (item)
            {
                viewModel.Works.remove(item);
            }.bind(viewModel);
            //---------------------end删除项---------------------
                viewModel.WorkSum = ko.computed(
                  function ()
                  {
                      var sum = 0;
                      var array = viewModel.Works();
                      $.each(array, function (index, item)
                      {
                          sum += parseFloat(item.Sum());
                      }
                      );
                      sum = sum * viewModel.PrjExtent.WorkRatio();
                      return sum.toFixed(2);
                  }
                  , viewModel);

                ko.applyBindings(viewModel);
                
                function RedirectToPrjDisplayPage()
                {
                    var disUrl = '@Url.Action("SetCurPrj","ProjectManage")';
                    $.post(disUrl, { 'prjId': viewModel.PrjId(), 'prjName': viewModel.PrjName() }, function (data)
                    {
                        if (data == "1")
                        {
                            var redirectUrl = '@Url.Action("Index","DoAssess")';
                            window.location.href = redirectUrl;
                        }
                        else
                        {
                            alert('erro in setting cur prj!');
                        }

                    });
                }

                //Form Submit
                $('#myForm').submit(function (event)
                {
                    event.preventDefault();
                    var jsonData = ko.toJSON(viewModel);
                    var url = '@Url.Action("CanDirectlySaveAssess", "ReadAssess")';
                    $.post(url, { 'prjJson': jsonData },
                        function (data)
                        {
                            var result = data;
                            if (result.IsSave == true)
                            {
                                ModalHelper.showDialog("保存成功", '测算信息已成功保存!', 1);
                                RedirectToPrjDisplayPage();

                            }
                            else
                            {
                                if (result.NeedReSave == false)
                                {
                                    ModalHelper.showDialog("提示", "材料和工作量均未超限,不能重算!", 1);
                                }
                                else
                                {
                                    ModalHelper.showDialog("提示", result.Msg+"?", 2, function ()
                                    {
                                        //由于材料埋设费或者工作量核定量超出限定
                                        //并且用户确认重新评测
                                        var ReSaveUrl = '@Url.Action("SaveAssessIntoDataBase", "ReadAssess")';
                                        $.post(ReSaveUrl, { 'prjJson': jsonData },
                                            function (data)
                                            {
                                                if (data == "1")
                                                {
                                                    ModalHelper.showDialog("保存成功", '测算信息已成功保存!', 1);
                                                    RedirectToPrjDisplayPage();
                                                }
                                                else
                                                {
                                                    ModalHelper.showDialog("保存失败", '测算信息未能保存!', 1);
                                                }
                                            });
                                    });
                                }
                                $('#dialog').modal('show');
                            }

                        });
                    return false;
                }
                );
                //上一步
                function GoPreview(targetUrl)
                {

                    var jsonData = ko.toJSON(viewModel);
                    var url = '@Url.Action("SaveAssess", "ReadAssess")';
                    $.post(url, { 'prjJson': jsonData },
                        function (data)
                        {
                            if (data = 1)
                            {
                                document.location.href = targetUrl;

                            }
                            else
                            {
                                alert('Err!');

                            }
                        });
                }
                $('#preview').click(function (event)
                {
                    event.preventDefault();
                    var url = '@Url.Action("DisplayMertialBury", "ReadAssess")';
                    GoPreview(url);
                }
                );
                $('.preview1').click(function (event)
                {
                    event.preventDefault();
                    var url = '@Url.Action("Display", "ReadAssess")';
                    GoPreview(url);
                }
                );
                $('.preview2').click(function (event)
                {
                    event.preventDefault();
                    var url = '@Url.Action("DisplayMertialBury", "ReadAssess")';
                    GoPreview(url);
                }
                );
                $('#noBtn').addClass('sr-only');

            })

    </script>
}

<br />
<div class="container">

    <ol class="breadcrumb">
        <li><a href="#" class="preview1">1.填写项目基本信息</a></li>
        <li><a href="#" class="preview2">2.填写项目材料埋设明细</a></li>
        <li class="active">3.填写项目工作量明细</li>
    </ol>
        @using (Html.BeginForm("SaveMertialBury", "ReadAssess", FormMethod.Post, new { id = "myForm", enctype = "multipart/form-data", @class = "form-horizontal" }))
        {
         
            <fieldset>
                <legend>材料埋设明细</legend>
                <table class="table table-bordered">
                    <thead>
                        <tr style="background-color: #169dd8;color:#fff">@* style="border-bottom: 3px solid #169dd8">*@
                            <th>分项内容</th>
                            <th>数量(个)</th>
                            <th>单价(￥)</th>
                            <th>小计(￥)</th>
                            <th>预估次数(次)</th>
                            <th>费用(￥)</th>
                            <th>*</th>
                        </tr>
                    </thead>
           
                        <tbody>
                            <!-- ko foreach: Works -->
                            <tr>
                                <td>
                                    <input required data-bind="value: ContentName" type="text" class="form-control" />

                                </td>
                                <td>
                                    <input required data-bind="value: Number" type="number" class="text-danger form-control" />
                                <td>
                                    <input required data-bind="value: Price" type="number" class="form-control" step="any" />
                                </td>
                                <td>
                                    <input required class="text-primary form-control" readonly="readonly" data-bind="value: PriceTotal" type="number" />
                                </td>
                                <td>
                                    <input required data-bind="value: PredictNo" type="number" class="form-control" />
                                </td>
                                <td>
                                    <input required class="text-primary form-control" readonly="readonly" data-bind="value: Sum" type="number" />
                                </td>
                                <td><a href='#' class="btn btn-sm btn-default" data-bind='click: $root.removeWorkItem'>删除</a></td>
                            </tr>
                            <!-- /ko -->
                        </tbody>
                    </table>

            </fieldset>
            <div class="row">
                <div class="col-lg-3">
                    <strong class="pull-left"><a href="#" data-bind='click: addWorkItem '>&nbsp;添加新项&nbsp;</a></strong>
                </div>
                <div class="col-lg-9">
                    <div class="pull-right">
                        <span class="text-muted">核定工作量:</span>
                        <strong class="text-info" data-bind="text: WorkSum"></strong>
                    </div>
                </div>
            </div>
            <br />
            <br />
              <div class="form-group">
                  <div class="row">
                      <div class="col-lg-offset-9 col-lg-3">
                          <div class="pull-right">
                              <a class="btn btn-primary" href="#" id="preview">上一步</a>&nbsp;
                             <input type="submit" class="btn  btn-danger" value="保 存" />&nbsp;
                          </div>
                      </div>
                  </div>

            </div>
        }
</div>
